<?php
namespace App\Shop\Forms\Service;

use App\Admin\Repositories\GoodOrderDetail;
use App\Models\GoodOrder;
use App\Models\ShopUser;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Grid;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class HandleGoodOrder extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         $res = 0;
         if ($input){
             try {
                 $oid = $input['oid'];
                 $order = GoodOrder::whereId($oid)->first();
                 $order->state = $input['state'];
                 if (isset($input['courier_com'])){
                     $order->courier_com = $input['courier_com'];
                 }
                 if (isset($input['courier_number'])){
                     $order->courier_number = $input['courier_number'];
                 }
                 if ($order->state==2){
                     $order->send_time = now()->format("Y-m-d H:i:s");
                 }
                 $res = $order->save();
             }catch (ModelNotFoundException|Exception $e){
                 return $this->response()->error($e->getMessage())->refresh();
             }
         }
         return $this->response()->success($res?'保存成功.':'暂无修改.')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $oid = $this->payload['oid'];
        $order = GoodOrder::whereId($oid)->first();
        $this->html($this->grid($order->order_no),"订单详情");
        $options = ShopUser::query()->select(['id','name','address'])->chunkMap(function ($item){
            $item->text = $item->name."【{$item->address}】";
            return $item;
        })->pluck("text","id");
        $this->display("amount");
        $this->display("address.name","收件人名称");
        $this->display("address.phone","收件人电话");
        $this->display("address.address","收件地址");
        $this->hidden("oid")->default($oid)->value($oid);
        $companys = config('platform.company');
        if ($order->method==0){
            $this->select("courier_com","快递公司")->options(collect($companys)->pluck("title","value"))->required();
            $this->text("courier_number","快递单号")->required();
        }
        $this->radio("state","发货状态")->options([1=>'待发货',2=>'确认发货'])->default(1);
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default(): array
    {
        return $this->getDefault();
    }

    protected function getDefault(){
        $oid = $this->payload['oid'];
        $order = GoodOrder::whereId($oid)->first();
        return $order->toArray();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($oid)
    {
        return Grid::make(new GoodOrderDetail(), function (Grid $grid) use ($oid) {
            $grid->disableCreateButton();
            $grid->disableColumnSelector();
            $grid->disableActions();
            $grid->disableRefreshButton();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->column('id')->sortable();
            $grid->model()->where("oid",$oid);
            $grid->column('good_id','商品ID');
            $grid->column('good_name','商品名称');
            $grid->column('good_image','商品图片')->image(null,40,40);
            $grid->column('good_number','商品数量');
            $grid->column('good_price','商品单价');
            $grid->column('total_price','小计');
        });
    }
}
